
package com.kanjian.music.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.support.v4.content.LocalBroadcastManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.kanjian.music.R;
import com.kanjian.music.api.ApiRequestManager;
import com.kanjian.music.constants.IntentConstants;
import com.kanjian.music.dialog.ShareDialog;
import com.kanjian.music.entity.BaseCode;
import com.kanjian.music.entity.Musician;
import com.kanjian.music.fragment.NoAvatarMusicListFragment;
import com.kanjian.music.menu.MusicMenuLayout;
import com.kanjian.music.network.HttpLoader;
import com.kanjian.music.util.FastBlur;
import com.kanjian.music.view.HexagonImageView;
import com.kanjian.music.view.RecyclingImageView;
import com.kanjian.music.volley.VolleyQueue;

/**
 * 音乐人主页
 * 
 * @author xuyanjun
 */
public class MusicianHomeActivity extends BaseActivity implements LoaderCallbacks<String> {

    private int mMusicianId;

    private Musician mMusician;

    private NoAvatarMusicListFragment mMusicListFragment;

    private RecyclingImageView mIv_background;

    private HexagonImageView mIv_musicianPic;

    private TextView mTv_musicianLocation;

    private TextView mTv_musicianStatus;

    private TextView mTv_musicianGenre;

    private LinearLayout mLayout_opertion;

    private Button mBtn_follow;

    private Button mBtn_share;

    private final int LOADERID_GETMUSICIAN = 0;

    private final int LOADERID_OPERTION = 1;

    private LocalBroadcastManager mLocalBroadcastManager;
    
    @Override
    protected void onCreate(Bundle arg0) {
        mIsContentViewSeted = false;
        super.onCreate(arg0);
        setContentView(R.layout.activity_musician_home);
        super.initBaseView();
        Intent intent = getIntent();
        mMusicianId = intent.getIntExtra(IntentConstants.MUSICIANID, -1);
        initView();
        if (mMusicianId != -1) {
            Bundle args = new Bundle();
            args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                    ApiRequestManager.getMusicianRequest(mMusicianId));
            getSupportLoaderManager().restartLoader(LOADERID_GETMUSICIAN, args, this);
        } else {
            finish();
        }
        mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
    }

    private void initView() {
        mMusicListFragment = (NoAvatarMusicListFragment) getSupportFragmentManager()
                .findFragmentById(R.id.musician_home_musiclist_fragment);
        mIv_background = (RecyclingImageView) findViewById(R.id.musician_home_background_image);
        mIv_musicianPic = (HexagonImageView) findViewById(R.id.musician_home_pic);
        mTv_musicianLocation = (TextView) findViewById(R.id.musician_home_musician_location);
        mTv_musicianGenre = (TextView) findViewById(R.id.musician_home_musician_genre);
        mTv_musicianStatus = (TextView) findViewById(R.id.musician_home_musician_status);

        mLayout_opertion = (LinearLayout) findViewById(R.id.musician_home_operation_layout);
        mBtn_follow = (Button) mLayout_opertion.findViewById(R.id.musician_home_operation_follow);
        mBtn_follow.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mMusician != null) {
                    Bundle args = new Bundle();
                    if (mMusician.mIsFan == 1) {
                        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                                ApiRequestManager.getDoFollowRequest(mMusicianId));
                    } else {
                        args.putSerializable(IntentConstants.HTTPLOADERREQUESTPARAM,
                                ApiRequestManager.getCancelFollowRequest(mMusicianId));
                    }
                    getSupportLoaderManager().restartLoader(LOADERID_OPERTION, args,
                            MusicianHomeActivity.this);
                }
            }
        });
        mBtn_share = (Button) mLayout_opertion.findViewById(R.id.musician_home_operation_share);
        mBtn_share.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                ShareDialog.newInstance(mMusician).show(getSupportFragmentManager(), "");
            }
        });
        
    }

    @Override
    public Loader<String> onCreateLoader(int id, Bundle args) {
        return new HttpLoader(this, args);
    }

    @Override
    public void onLoadFinished(Loader<String> loader, String data) {
        if (data != null) {
            if (loader.getId() == LOADERID_GETMUSICIAN) {
                mMusician = Musician.getMusicianFromJson(data);
                if (mMusician != null && mMusician.mMusicList != null) {
                    mLayout_opertion.setVisibility(View.VISIBLE);
                    mIv_musicianPic.setImageUrl(mMusician.mUserPic, VolleyQueue.getImageLoader());

                    VolleyQueue.getImageRequestQueue().add(
                            new ImageRequest(mMusician.mUserPic, new Response.Listener<Bitmap>() {
                                @Override
                                public void onResponse(Bitmap bmp) {
                                    mIv_background.setImageBitmap(FastBlur.doBlur(bmp, 50, false));
                                }
                            }, 0, 0, Config.ARGB_8888, null));

                    setTitle(mMusician.mUserName);
                    mTv_musicianLocation.setText(mMusician.mUserLocation);
                    mTv_musicianGenre.setText(mMusician.getGenreString());
                    mTv_musicianStatus.setText(mMusician.mMusicianStatus);
                    mMusicListFragment.setMusicList(mMusician.mMusicList);
                    mBtn_follow.setText(mMusician.mIsFan == 1 ? "添加关注" : "取消关注");
                }
            } else if (loader.getId() == LOADERID_OPERTION) {
                BaseCode code = BaseCode.getCommonCodeFromString(data);
                if (code.isOKCode()) {
                    mMusician.mIsFan = mMusician.mIsFan == 0 ? 1 : 0;
                    mBtn_follow.setText(mMusician.mIsFan == 1 ? "添加关注" : "取消关注");

                    if (mMusician.mIsFan == 1) {
                        Intent intent = new Intent(IntentConstants.ACTION_CANCEL_FOLLOW_MUSICIAN);
                        intent.putExtra(IntentConstants.MUSICIANID, mMusician.mUserId);
                        mLocalBroadcastManager.sendBroadcast(intent);
                    }
                }
            }
        }
    }

    @Override
    public void onLoaderReset(Loader<String> loader) {
    }

}
